package com.stormrage.edu.serv.exam.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.stormrage.constants.ErrCons;
import com.stormrage.edu.dao.exam.ExmRelateCountDao;
import com.stormrage.edu.serv.exam.ExmExamServ;
import com.stormrage.edu.serv.exam.ExmRelateCountServ;
import com.stormrage.entity.exam.ExmRelateCount;
import com.stormrage.entity.exam.rep.ExmExamCountRep;
import com.stormrage.entity.exam.req.ExmExamReq;
import com.stormrage.entity.exam.req.ExmRelateCountReq;
import com.stormragetech.core.exception.StormrageTechException;

/**
 * @Title: ExmRelateExamServImpl.java
 * @Package com.stormrage.edu.serv.exam.impl
 * @Description: 试题关联分数，数量 Service
 * @author www.stormragetech.com
 * @date 2016年4月22日
 * @version V1.0
 */
@Service
public class ExmRelateCountServImpl implements ExmRelateCountServ {

	private static final Logger logger = LoggerFactory.getLogger(ExmScoreServImpl.class);

	@Autowired
	private ExmExamServ exmExamServ;

	@Autowired
	private ExmRelateCountDao exmRelateExamDao;

	@Override
	public Integer create(final ExmRelateCount arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 ExmRelateExamServ 】- 【 create 】-【 STARTED 】");

		Integer index = exmRelateExamDao.create(arg0);

		logger.debug(" 【 Serv 】- 【 ExmRelateExamServ 】- 【 create 】-【 COMPLETED 】");
		return index;

	}

	@Override
	public void update(final ExmRelateCount arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 ExmRelateExamServ 】- 【 update 】-【 STARTED 】");

		ExmExamReq examReq = new ExmExamReq(arg0.getType(), arg0.getRelateId());
		ExmExamCountRep rep = exmExamServ.queryRepByRelate(examReq);
		if (arg0.getSele() > rep.getSele()) {
			throw StormrageTechException.instance(ErrCons.SERV_ERR, "单选题数量大于现有题目数量", StormrageTechException.ERR_APP,
					"【 ExmRelateExamServ 】- 【 update 】");
		}
		if (arg0.getMult() > rep.getMult()) {
			throw StormrageTechException.instance(ErrCons.SERV_ERR, "多选题数量大于现有题目数量", StormrageTechException.ERR_APP,
					"【 ExmRelateExamServ 】- 【 update 】");
		}
		if (arg0.getCorr() > rep.getCorr()) {
			throw StormrageTechException.instance(ErrCons.SERV_ERR, "判断题数量大于现有题目数量", StormrageTechException.ERR_APP,
					"【 ExmRelateExamServ 】- 【 update 】");
		}

		ExmRelateCountReq req = ExmRelateCountReq.instance(arg0.getTermId(), arg0.getRelateId(), arg0.getType());
		ExmRelateCount entity = queryByTermRelateType(req);

		if (null == entity)
			create(arg0);
		else {
			arg0.setId(entity.getId());
			exmRelateExamDao.update(arg0);
		}

		logger.debug(" 【 Serv 】- 【 ExmRelateExamServ 】- 【 update 】-【 COMPLETED 】");

	}

	@Override
	public ExmRelateCount queryById(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 ExmRelateExamServ 】- 【 queryById 】-【 STARTED 】");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		ExmRelateCount entity = exmRelateExamDao.queryById(map);

		logger.debug(" 【 Serv 】- 【 ExmRelateExamServ 】- 【 queryById 】-【 COMPLETED 】");
		return entity;

	}

	@Override
	public ExmRelateCount queryByTermRelateType(final ExmRelateCountReq arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 ExmRelateExamServ 】- 【 queryByTermRelateType 】-【 STARTED 】");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("termId", arg0.getTermId());
		map.put("relateId", arg0.getRelateId());
		map.put("type", arg0.getType());

		ExmRelateCount entity = exmRelateExamDao.queryByTermRelateType(map);

		logger.debug(" 【 Serv 】- 【 ExmRelateExamServ 】- 【 queryByTermRelateType 】-【 COMPLETED 】");
		return entity;

	}

	@Override
	public List<ExmRelateCount> queryList() throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 ExmRelateExamServ 】- 【 queryList 】-【 STARTED 】");

		List<ExmRelateCount> array = exmRelateExamDao.queryList();

		logger.debug(" 【 Serv 】- 【 ExmRelateExamServ 】- 【 queryList 】-【 COMPLETED 】");
		return array;

	}

}
